<template>
  <div class="seller" ref="sellerBox">
    <div class="seller-content">
      <div class="overview">
        <div class="title">{{seller.name}}</div>
        <div class="desc border-1px">
          <v-star :size="36" :score="seller.score"></v-star>
          <span class="text">({{seller.ratingCount}})</span>
          <span class="text">月售{{seller.sellCount}}单</span>
        </div>
        <ul class="remark">
          <li class="block">
            <span class="title">起送价</span>
            <div class="price"><sub>{{seller.minPrice}}</sub>元</div>
          </li>
          <li class="block">
            <span class="title">商家配送</span>
            <div class="price"><sub>{{seller.deliveryPrice}}</sub>元</div>
          </li>
          <li class="block">
            <span class="title">平均配送时间</span>
            <div class="price"><sub>{{seller.deliveryTime}}</sub>分钟</div>
          </li>
        </ul>
        <div class="favorite" @click="_toggleFavorite">
          <i class="icon-favorite" :class="{active: flag}"></i>
          <span class="text">{{favoriteText}}</span>
        </div>
      </div>
      <v-split></v-split>
      <div class="notice-activity">
        <h2>活动与公告</h2>
        <div class="notice">{{seller.bulletin}}</div>
        <ul class="support-list" v-if="seller.supports">
          <li class="support-item border-1px" v-for="(item, index) in seller.supports" :key="index">
            <span class="icon" :class="classMap[item.type]"></span>
            <span class="text">{{item.description}}</span>
          </li>
        </ul>
      </div>
      <v-split></v-split>
      <div class="seller-pics">
        <h2>商家实景</h2>
        <div class="pic-wrapper" ref="picWrapper">
          <ul v-show="seller.pics" ref="picList">
            <li v-for="(pic, index) in seller.pics" :key="index">
              <img :src="pic" width="120" height="90" alt="" />
            </li>
          </ul>
        </div>
      </div>
      <v-split></v-split>
      <div class="seller-infos">
        <h2>商家信息</h2>
        <ul class="support-list" v-if="seller.infos">
          <li class="support-item border-1px" v-for="(item, index) in seller.infos" :key="index">{{item}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Star from '@/components/star';
import Split from '@/components/split';
import BScroll from 'better-scroll';

export default {
  components: {
    'v-star': Star,
    'v-split': Split
  },
  props: {
    seller: {
      type: Object
    }
  },
  data () {
    return {
      flag: false
    };
  },
  created () {
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
  },
  mounted () {
    this._initScroll();
    this._initPicScroll();
  },
  watch: {
    seller () {
      this._initScroll();
      this._initPicScroll();
    }
  },
  computed: {
    favoriteText () {
      return this.flag ? '已收藏' : '收藏';
    }
  },
  methods: {
    _initScroll () {
      if (!this.scroll) {
      this.scroll = new BScroll(this.$refs.sellerBox, {
          click: true
        });
      } else {
        this.scroll.refresh();
      }
    },
    _initPicScroll () {
      if (this.seller.pics) {
        let picW = 120;
        let marginR = 6;
        let width = (picW + marginR) * this.seller.pics.length - marginR;
        this.$refs.picList.style.width = width + 'px';
        if (!this.picScroll) {
          this.picScroll = new BScroll(this.$refs.picWrapper, {
            scrollX: true,
            eventPassthrough: 'vertical'
          });
        } else {
          this.picScroll.refresh();
        }
      }
    },
    _toggleFavorite () {
      this.flag = !this.flag;
    }
  }
};
</script>

<style lang="less" scoped>
 @import './res/style.less';
</style>